﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace ENG.TeachersDiary
{
  public partial class FrmInsertStudents : Form
  {
    public FrmInsertStudents()
    {
      InitializeComponent();
    }

    public List<Model.Student> GetNewStudents()
    {
      List<Model.Student> ret = new List<Model.Student>();

      string regex;
      bool isNameFirst = false;
      switch(cmbStyle.SelectedIndex)
      {
        case 0:
          regex = @"(.*) (.*)";
          isNameFirst = true;
          break;
        case 1:
          regex = @"(.*?) (.*)";
          isNameFirst = false;
          break;
        case 2:
          regex = @"(.*?), (.*)";
          isNameFirst = false;
          break;
        default:
          throw new NotSupportedException();
      }

      System.IO.StringReader str = new System.IO.StringReader(txtNames.Text);

      string line = str.ReadLine();

      while (line != null)
      {
        line = line.Trim();
        if (line.Length > 0)
        {
          Match m = Regex.Match(line, regex);
          if (m.Success)
          {
            string a = m.Groups[1].Value;
            string b = m.Groups[2].Value;

            Model.Student st = new Model.Student();
            if (isNameFirst)
            {
              st.Name = a;
              st.Surname = b;
            }
            else
            {
              st.Name = b;
              st.Surname = a;
            }

            ret.Add(st);
          } // if (m.Success)
        }
        line = str.ReadLine();
      }

      return ret;
    }

    private void FrmInsertStudents_Load(object sender, EventArgs e)
    {
      cmbStyle.SelectedIndex = 1;
    }
  }
}
